#ifndef TARGETVIEW_H
#define TARGETVIEW_H

#include "rightwidget.h"
#include "target.h"
#include "QextSerialPort/qextserialenumerator.h"
#include "QextSerialPort/qextserialport.h"

enum DatagramType
{
    dgtUdp=0,
    dgtSend=1,
    dgtRecv=2
};

#define MAX_DATAGRAM_COUNT    2048

class TargetDatagramView : public QWidget
{
    Q_OBJECT
public:
    TargetDatagramView(QWidget* parent=0);

public:
    void addDatagram(const QDateTime& time, DatagramType type,
                     const QString& address,const QByteArray& data);
    void clear();

    void setAutoScroll(bool autoScroll){m_autoScroll=autoScroll;}

protected:
    void updateDatagramList();

protected slots:
    void updateContent(int row,int);

private:
    QTableWidget* m_twDatagram;
//    QLabel* m_lbContent;
    QPlainTextEdit* m_content;

    QList<QDateTime> m_lstTime;
    QList<DatagramType> m_lstType;
    QStringList m_lstAddress;
    QStringList m_lstData;

    int m_curRow;

    bool m_autoScroll;
};

typedef struct _tHy103ServerInfo
{
    quint8 type;
    QList<QDateTime> lstDgTime;
    QList<quint8> lstType;
    QList<QByteArray> lstDgData;
}Hy103ServerInfo;

class TargetHy103View : public QWidget
{
    Q_OBJECT
public:
    TargetHy103View(QWidget* parent=0);
    virtual ~TargetHy103View();

public:
    void setData(const QByteArray& data);

    bool load(const QString& fileName);
    bool save(const QString& fileName);

protected slots:
    void curServerSelectChanged(int row,int);
    void datagramBackgroundChanged();
    void clearDatagram();

    void datagramFilterChanged();
    void datagramAutoScroll();

protected:
    void updateServerInfo();

private:
    QCheckBox* m_cbDatagramBackground;
    QTableWidget* m_twServer;

    QCheckBox* m_cbUdp;
    QCheckBox* m_cbTcpSend;
    QCheckBox* m_cbTcpRecv;
    QCheckBox* m_cbAutoScroll;
    QPushButton* m_pbClearDatagram;
    TargetDatagramView* m_tdvDatagram;

    quint32 m_clientIp;
    quint16 m_clientUdpPort,m_clientTcpPort;
    QMap<quint32,Hy103ServerInfo*> m_server;

    quint32 m_curIp;

    bool m_datagramBackground;
};

#define CFM_SENDFLAG       0x10000000
#define CFM_SERVERTYPE     0x03000000
#define CFM_DIRECTION      0x00800000
#define CFM_CANADDR        0x00000001F

class DialogCanFilter : public QDialog
{
    Q_OBJECT
public:
    DialogCanFilter(quint32 filterMask,quint32 filter,QWidget* parent=0);
    virtual ~DialogCanFilter();

public:
    void getFilter(quint32& filterMask,quint32& filter);
    void getFilter(bool& interact,bool& autoSend,int& serverType,bool& up,bool& down,int& canAddr);

protected slots:
    void sendTypeChanged(int);
    void directionChanged(int);
    void serverTypeChanged(int);
    void canAddrEnableChanged(int);
    void canAddrChanged(const QString& text);

private:
    void parseFilter();

private:
    QComboBox* m_cbSendType;
    QComboBox* m_cbServerType;
    QComboBox* m_cbDirection;
    QCheckBox* m_cbCanAddr;
    QLineEdit* m_leCanAddr;

    QLabel* m_lbFilterMask;
    QLabel* m_lbFilter;

    QPushButton* m_pbOk;
    QPushButton* m_pbCancel;

    quint32 m_filterMask;
    quint32 m_filter;
    bool m_interact;
    bool m_autoSend;
    int m_serverType;
    bool m_up;
    bool m_down;
    int m_canAddr;
};

class TargetCanView : public QWidget
{
    Q_OBJECT
public:
    TargetCanView(QWidget* parent=0);
    virtual ~TargetCanView();

public:
    void setData(const QByteArray& data);

    bool load(const QString& fileName);
    bool save(const QString& fileName);

protected slots:
    void filterSet();
    void clearDatagram();
    void datagramAutoScroll();

private:
    QLabel* m_lbFilter;
    QPushButton* m_pbFilter;

    QCheckBox* m_cbAutoScroll;
    QPushButton* m_pbClearDatagram;
    TargetDatagramView* m_tdvDatagram;

    QList<QDateTime> m_lstDgTime;
    QList<quint8> m_lstType;
    QList<quint32> m_lstDgId;
    QList<QByteArray> m_lstDgData;

    quint32 m_curFilterMask;
    quint32 m_curFilter;
};

class TargetRs485Test : public QWidget
{
    Q_OBJECT
public:
    TargetRs485Test(QWidget* parent=0);
    virtual ~TargetRs485Test();

protected slots:
    void startTest();
    void stopTest();
    void recvData();

    void testTimeout();

private:
    QComboBox* m_cbPort;
    QComboBox* m_cbBitrate;
    QComboBox* m_cbDatabits;
    QComboBox* m_cbStopbits;
    QComboBox* m_cbParity;

    QLabel* m_lbDatagram;

    QextSerialPort* m_port;
    QList<QextPortInfo> m_lstPorts;

    QPushButton* m_pbStart;
    QPushButton* m_pbStop;

    QTimer* m_timerTest;

    int m_recvLen;
    int m_recvTotal;
    char m_recvBuf[256];
};

enum TargetModule
{
    tmEth0=1,
    tmEth1=2,
    tmCan0=3,
    tmCan1=4
};

class TargetView : public RightWidget
{
    Q_OBJECT
public:
    TargetView(TargetManager* target,const QString& path);
    virtual ~TargetView();

public:
    void setTarget(TargetManager* target){m_target=target;}
    TargetModule curModule();
    bool startDebug();
    bool stopDebug();

    virtual bool init();
    virtual bool save();

public slots:
    void processMonitorData(QByteArray data);

    void viewChanged(int index);

private:
    QTabWidget* m_tabModule;

    TargetHy103View* m_viewHy103Eth0;
    TargetHy103View* m_viewHy103Eth1;

    TargetCanView* m_viewCan0;
    TargetCanView* m_viewCan1;

    TargetRs485Test* m_rs485Test;

    TargetManager* m_target;
    tgtFuncCode m_curFuncCode;
    bool m_debugStart;
};

#endif // TARGETVIEW_H
